<template>
	<view class="height-100 bg-or overflow-hidden">
		<!-- 如果是开处方过来的，需要带上患者、处方信息 -->
		<template v-if="ifPrescribing">
			<view class="flex-direction-row justify-between padding bg-white">
				<text class="text-smd color-df">NO.{{prescribingData.prescriptionCode}}</text>
				<text class="text-sm color-black-q">{{prescribingData.createTime}}</text>
			</view>
			<!-- 患者资料 -->
			<view class="bg-white radius-xl padding margin">
				<view class="flex-direction-row justify-between">
					<view class="">
						<text class="text-df text-bold">患者：</text>
						<text class="text-df color-black-q">{{prescribingData.name}}</text>
					</view>
					<view class="">
						<text class="text-df text-bold">性别：</text>
						<text class="text-df color-black-q">{{prescribingData.gender === 0 ? '男' : '女'}}</text>
					</view>
					<view class="">
						<text class="text-df text-bold">年龄：</text>
						<text class="text-df color-black-q">{{prescribingData.age}}</text>
					</view>
				</view>
			</view>
		</template>
		<!--  -->
		<view class="bg-white radius-xl padding margin">
			<view class="">
				<text class="text-df text-bold">处方医师：</text>
				<text class="text-sm color-black-q">{{userinfo.doctorName}}</text>
			</view>
			<view class="margin-top">
				<text class="text-df text-bold">科室：</text>
				<text class="text-sm color-black-q">{{userinfo.department}}</text>
			</view>
		</view>
		<!-- 诊断意见 -->
		<view class="bg-white radius-xl padding margin flex-direction-column">
			<text class="text-df color-green padding-tb-xs">初步诊断：</text>
			<textarea v-model="primaryDiagnosis" style="height: 100rpx; width: 590rpx;" class="border-gray padding-sm radius"
				placeholder="请输入诊断信息~"></textarea>
			<text class="text-df color-green padding-tb-xs">处理意见：</text>
			<textarea v-model="processAdvice" style="height: 100rpx; width: 590rpx;" class="border-gray padding-sm radius"
				placeholder="请输入处理意见~"></textarea>
		</view>
		<!-- 配药 -->
		<view class="bg-white radius-xl padding margin">
			<view class="padding-tb">
				<text class="text-lg color-df">配药</text>
			</view>
			<!-- 用户自选药品 -->
			<view class="padding bg-gray radius-xl margin-tb">
				<text class="text-smd">用户自选药品</text>
				<view class="flex-direction-column margin-tb-sm">
					<view class="flex-direction-row justify-between">
						<text class="text-df text-bold flex-sub">{{productItemList.productName}}</text>
						<text class="text-sm color-black-q margin-left flex-shrink">x {{productItemList.count}}</text>
					</view>
					<text class="text-smd color-black-q margin-top-xsm">规格：{{productItemList.productSpaceName}}</text>
				</view>
			</view>
			<block v-for='(item,index) in prescriptionProductItemList' :key='index'>
				<view class="border-bottom padding-tb">
					<view class="flex-direction-column">
						<view class="flex-direction-row justify-between">
							<text class="text-df text-bold flex-sub">{{item.productName}}</text>
							<text class="text-sm color-black-q margin-left flex-shrink">x {{item.count}}</text>
						</view>
						<text class="text-smd color-black-q margin-top-xsm">规格：{{item.productSpaceName}}</text>
						<text
							class="text-smd color-black-b margin-top-xsm">{{medicationUsageArray[item.medicationUsage]}}：{{usageFrequencyArray[item.usageFrequency]}} {{unitArray[item.unit]}}</text>
					</view>
					<view class="flex-direction-row justify-end" @click="delMethod(index)">
						<text class="text-xsl color-df iconfont padding-xs">&#xe622;</text>
					</view>
				</view>
			</block>
			<view class="flex-direction-column justify-center align-center margin-top">
				<!-- <text class="text-smd color-black padding-tb-sm" >暂无配药信息~</text> -->
				<view class="flex-direction-row justify-center align-center border-green radius-sl text-center"
					@click="$navigator('/pages/mall/mall?storeId='+storeId)" style="width: 240rpx; height: 60rpx;">
					<text class="text-sm color-green">+ 新增药品</text>
				</view>
			</view>
		</view>
		<!-- 查看原始处方 -->
		<!-- <view class="flex-direction-row justify-center margin-tb col-1">
			<view class="border-df padding-lr-xl padding-tb-sm radius-sl" @click="openPopup">
				<text class="text-df color-df">查看原始处方</text>
			</view>
		</view> -->
		<!-- 底部操作 -->
		<view style="height: 200rpx;"></view>
		<view class="position-fixed-bottom bg-white border-top">
			<view class="padding" @click="submit">
				<view class="bg-df radius-sl padding-tb-sm text-center">
					<text class="text-df color-white">确定</text>
				</view>
			</view>
			<view style="height: 60rpx;"></view>
		</view>
	</view>
</template>

<script>
	import {
		fields
	} from "@/common/fields.js"
	export default {
		onLoad(option) {
			// console.log('option: ',option);
			this.userinfo = uni.getStorageSync('userinfo')
			// 如果prescribingData存在，即是从开处方过来的，反之则是处方管理过来的
			if (option.prescribingData) {
				// 如果来源是开处方
				this.ifPrescribing = true;
				this.prescribingData = JSON.parse(option.prescribingData);
				console.log('this.prescribingData: ',this.prescribingData);
				this.storeId = this.prescribingData.storeId;
				console.log('storeId: ',this.storeId);
				// 用户自选药
				this.productItemList = this.prescribingData?.prescriptionProductItemList[0]
			}
			uni.$on('selectMethod', data => {
				console.log('selectMethod data: ', data);
				this.prescriptionProductItemList.push(data)
				// console.log('this.prescriptionProductItemList.length',this.prescriptionProductItemList.length);
			})
		},
		onUnload() {
			uni.$off('selectMethod');
		},
		data() {
			return {
				storeId:'',//门店id，从聊天页面consultation带过来
				userinfo: {},
				processAdvice: '',
				primaryDiagnosis: '',
				productItemList: [], //原始处方里用户自选药品
				prescriptionProductItemList: [],//医师选的药品
				ifPrescribing: false,
				prescribingData: {},
				usageFrequencyArray: fields.usageFrequency,
				unitArray: fields.unit,
				medicationUsageArray: fields.medicationUsage,
			}
		},
		methods: {
			// 查看原始处方
			openPopup() {
				this.$refs.originalPopup.open();
			},
			closePopup() {
				this.$refs.originalPopup.close();
			},
			delMethod(index) {
				this.prescriptionProductItemList.splice(index, 1);
			},
			async submit() {
				if (this.primaryDiagnosis === '') {
					uni.showToast({
						title: '诊断信息不能为空',
						icon: 'none',
					});
					return;
				}
				if (this.processAdvice === '') {
					uni.showToast({
						title: '处理意见不能为空',
						icon: 'none',
					});
					return;
				}
				let url;
				let data = {
					prescriptionProductItemList: this.prescriptionProductItemList, //选药
					processAdvice: this.processAdvice, //处理意见
					primaryDiagnosis: this.primaryDiagnosis //初步诊断
				}
				// 保存处方信息
				if(this.ifPrescribing){
					// 开处方来的,不保存到常用，直接完成开方（编辑开方接口）操作
					url = '/doctorPrescriptionApi/savePrescription'
					data.id = this.prescribingData.id
				}else{
					// 保存为常用处方
					url = '/prescriptionDoctorApi'
				}
				const res = await uni.$request.post(url, data);
				console.log('submit 开方 res: ', res.data);
				if (res.code === 200) {
					// 通知处方list更新页面
					uni.$emit('updateSavePrescription');
					if(this.ifPrescribing){
						// 开处方来的、返回两层
						uni.navigateBack({
							delta: 2
						});
					}else{
						// 处方管理，返回一层
						uni.navigateBack({
							delta: 1
						});
					}
				} else {
					uni.showModal({
						title: '提示',
						content: '开方失败',
						showCancel: false
					});
				}
			}
		}
	}
</script>

<style scoped>
	.border-green {
		border: 1rpx solid #4CAF50;
	}
</style>